//
//  FBContactInfoHandle.m
//  UILbrary
//
//  Created by Apple on 2023/3/4.
//  Copyright © 2023 mac. All rights reserved.
//

#import "FBContactInfoHandle.h"


@interface FBContactInfoView : UIView

@property(nonatomic, strong) UIStackView *stackView;
@property(nonatomic, strong) UILabel *label;
@property(nonatomic, strong) UITextField *textfield;

@end


@implementation FBContactInfoView

//MARK: - system

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self p_configSubviews];
//        [self test];
    }
    return self;
}

//MARK: - test

- (void)test
{
    self.label.backgroundColor = [UIColor systemCyanColor];
    self.textfield.backgroundColor = [UIColor systemRedColor];
}

//MARK: - private

- (void)p_configSubviews
{
    [self addSubview:self.stackView];
    UIView *view_1 = [UIView new];
    [view_1 setContentCompressionResistancePriority:UILayoutPriorityRequired
                                            forAxis:UILayoutConstraintAxisHorizontal];
    [_stackView addArrangedSubview:self.label];
    [_stackView addArrangedSubview:view_1];
    [_stackView addArrangedSubview:self.textfield];
    
    [self.textfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(100);
    }];
    
    [self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self).insets(UIEdgeInsetsMake(0, Space_big, 0, Space_big));
    }];
}

//MARK: - get

- (UIStackView *)stackView
{
    if(!_stackView){
        _stackView = [UIStackView new];
        _stackView.axis = UILayoutConstraintAxisHorizontal;
        _stackView.distribution = UIStackViewDistributionFill;
        _stackView.alignment = UIStackViewAlignmentCenter;
        _stackView.spacing = 10;
    }
    return _stackView;
}

- (UILabel *)label
{
    if(!_label){
        _label = [UILabel new];
        _label.text = @"联系方式";
        _label.textColor = [UIColor blackColor];
        _label.font = [UIFont systemFontOfSize:14.0];
        _label.textAlignment = NSTextAlignmentLeft;
    }
    return _label;
}

- (UITextField *)textfield
{
    if(!_textfield){
        _textfield = [UITextField new];
        UIColor *color = [UIColor JX_GrayColor_5];
        NSString *placeholderText =  @"请输入手机号";
        NSAttributedString *attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholderText attributes:@{NSForegroundColorAttributeName: color}];
        _textfield.attributedPlaceholder = attributedPlaceholder;
        _textfield.textColor = [UIColor blackColor];
        _textfield.font = [UIFont systemFontOfSize:14.0];
        _textfield.keyboardType = UIKeyboardTypePhonePad;
    }
    return _textfield;
}


@end



@interface FBContactInfoHandle()

@property(nonatomic, strong) FBContactInfoView *p_view;

@end


@implementation FBContactInfoHandle

//MARK: - system

- (instancetype)init
{
    self = [super init];
    if(self){
        
    }
    return self;
}

//MARK: - private

- (void)showParamError
{
    [UIViewController showAelrtWithTitle:@"参数错误，请开发自查"];
    assert(NO);
}

- (void)showPhoneNumError
{
    [UIViewController showAelrtWithTitle:@"请先输入正确手机号"];
}


- (void)showTextNil
{
    [UIViewController showAelrtWithTitle:@"请先输入手机号"];
}

- (NSDictionary *)netRequestParam:(NSObject *)obj
{
    NSDictionary *tmp_dic = (NSDictionary *)obj;
    if(!tmp_dic){
        tmp_dic = @{};
    }
    NSMutableDictionary *mutableDic = [tmp_dic mutableCopy];
    [mutableDic addEntriesFromDictionary:tmp_dic];
    mutableDic[@"phone"] = self.p_view.textfield.text;
    
    return [mutableDic copy];
}

//MARK: - FBCompleteProtocol


- (void)completeHandle:(NSObject *)obj
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.view endEditing:YES];
    });
    
    if(!self.p_view.textfield.text ||
       self.p_view.textfield.text.length <= 0){
        [self showTextNil];
        return;
    }
    
    if(![self.p_view.textfield.text isValidPhoneNumberFormat]){
        [self showPhoneNumError];
        self.p_view.textfield.text = @"";
        return;
    }
    
    [self.nextStep completeHandle:[self netRequestParam:obj]];
}

//MARK: - get

- (FBContactInfoView *)p_view
{
    if(!_p_view){
        _p_view = [FBContactInfoView new];
    }
    return _p_view;
}

- (UIView *)view
{
    return self.p_view;
}

@end
